Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::operator[]

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
basic_node& operator[](KeyType&& key); // (1)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
const basic_node& operator[](KeyType&& key) const; // (2)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
basic_node& operator[](KeyType&& key); // (3)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
const basic_node& operator[](KeyType&& key) const; // (4)

Access to a YAML node element with either an index (for sequences) or a key (for mappings).
If the node is neither a sequence nor a mapping, or if the node is a sequence but the given key is not an integer, a fkyaml::type_error will be thrown.

Danger

This API does not check the size of a sequence node before accessing the element.
To avoid undefined behaviors, please make sure the argument key is smaller than the actual sequence size with a return value of the basic_node::size() function.
If bounds check is necessary, prefer basic_node::at() function.

Warning

This API does not check the existence of the given key in a mapping node.
If the key does not exist, a default basic_node object will be created.
Please make sure that the node has the given key in advance by calling the basic_node::contains() function.
If such a behavior is unwanted, prefer basic_node::at() function.

Overload (1), (2)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
basic_node& operator[](KeyType&& key); // (1)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
const basic_node& operator[](KeyType&& key) const; // (2)

Accesses to an element in the YAML sequence/mapping node with the given key object of a compatible type with the basic_node class, i.e., a type with which a basic_node object is constructible.
These overloads internally construct a basic_node object with key.

Parameters

index [in]
An index/key for an element in the YAML sequence/mapping node.

Return Value

Reference, or constant reference, to the YAML node object associated with the given index/key.

Example
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML sequence node.
    fkyaml::node n1 = {123, 234, 345, 456};

    // print YAML nodes at the following indexes.
    std::cout << n1[0] << std::endl;
    std::cout << n1[1] << std::endl;
    std::cout << n1[2] << std::endl;
    std::cout << n1[3] << std::endl;

    // this will cause an undefined behavior!
    // std::cout << n1[4] << std::endl;

    // create a YAML mapping node.
    fkyaml::node n2 = {{"foo", true}, {"bar", 123}};

    // print YAML nodes associated with the following keys.
    std::cout << std::boolalpha << n2["foo"] << std::endl;
    std::cout << n2["bar"] << std::endl;

    // try to access a YAML node with a key which does not exist.
    std::cout << n2[true] << std::endl;

    return 0;
}

output:

123
234
345
456
true
123
null

Overload (3), (4)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
basic_node& operator[](KeyType&& key); // (3)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
const basic_node& operator[](KeyType&& key) const; // (4)

Accesses to an element in the YAML sequence/mapping node with the given basic_node key object.
Unlike the overloads (1) and (2) above, these overloads do not internally construct a basic_node object.
So, these overloads works more effectively when some key objects are used multiple times, for instance, in a for-loop.

Template Parameters

KeyType
A key type which is a kind of the basic_node template class.

Parameters

key [in]
An index/key for an element in the YAML sequence/mapping node.

Return Value

Reference, or constant reference, to the YAML node object associated with the given index/key.

Example
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML sequence node.
    fkyaml::node n1 = {123, 234, 345, 456};

    // print YAML nodes at the following indexes.
    fkyaml::node index_zero = 0;
    fkyaml::node index_one = 1;
    fkyaml::node index_two = 2;
    fkyaml::node index_three = 3;
    std::cout << n1[index_zero] << std::endl;
    std::cout << n1[index_one] << std::endl;
    std::cout << n1[index_two] << std::endl;
    std::cout << n1[index_three] << std::endl;

    // this will cause an undefined behavior!
    // fkyaml::node index_four = 4;
    // std::cout << n1[index_four] << std::endl;

    // create a YAML node.
    fkyaml::node n2 = {{"foo", true}, {"bar", 123}};

    // print YAML nodes associated with the following keys.
    fkyaml::node foo_key = "foo";
    fkyaml::node bar_key = "bar";
    std::cout << std::boolalpha << n2[foo_key] << std::endl;
    std::cout << n2[bar_key] << std::endl;

    // try to access a YAML node with a key which does not exist.
    fkyaml::node true_key = true;
    std::cout << n2[true_key] << std::endl;

    return 0;
}

output:

123
234
345
456
true
123
null

See Also